package saint.bicyletracker.UI;

import java.lang.reflect.TypeVariable;

import android.app.Activity;
import android.content.*;
import android.graphics.*;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.*;
import android.widget.*;
import saint.manager.DPIConvert;


public class SpeedDetail extends ViewGroup {

 	private String mSpeedValue;
 	private String mUnit;
	
 	private RoundRectange backgroud;
 	private TextView speedLabel;
 	private TextView speedValue;
 	private TextView speedUnit;
 	private PointerLayout labelPointer;
 	private PointerLayout valuePointer;
 	private PointerLayout unitPointer;
 	
	public SpeedDetail(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}
	
	public void setSpeedText(String value) {
		speedValue.setText(value);
	}
	
	public void setUnit(String value) {
		speedUnit.setText(value);
	}
	
	private void init() {
		
		LayoutParams layoutSize = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		
		// Background init
		backgroud = new RoundRectange(getContext());
		// Text init
		speedLabel = new TextView(getContext());
		speedValue = new TextView(getContext());
		speedUnit = new TextView(getContext());
		
		labelPointer = new PointerLayout();
		valuePointer = new PointerLayout();
		unitPointer = new PointerLayout();
		
		speedLabel.setText("Speed");
		speedLabel.setTextSize(20);
		labelPointer.startX = (int)DPIConvert.getDPI(10, getContext());
		labelPointer.startY = (int)DPIConvert.getDPI(0, getContext());
		
		speedValue.setText("00.00");
		speedValue.setTextSize(30);
		speedValue.setGravity(Gravity.CENTER);
		speedValue.setLayoutParams(layoutSize);
		valuePointer.startX = 0;
		valuePointer.startY = (int)DPIConvert.getDPI(20, getContext());
		
		speedUnit.setText("Unit");
		speedUnit.setTextSize(16);
		speedUnit.setGravity(Gravity.RIGHT | Gravity.BOTTOM);
		speedUnit.setLayoutParams(layoutSize);
		speedUnit.setPadding(0, 0, 20, 5);
		unitPointer.startY = (int)DPIConvert.getDPI(55, getContext());
		
		
		addView(backgroud);
		addView(speedLabel);
		addView(speedValue);
		addView(speedUnit);
	}

	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		// Do not anything
	}
	
	@Override
	protected void onSizeChanged(int w, int h, int oldw, int oldh ) {
		super.onSizeChanged(w, h, oldw, oldh);
		backgroud.layout(0, 0, w, h);
		speedLabel.layout(labelPointer.startX, labelPointer.startY, w, h);
		speedValue.layout(valuePointer.startX, valuePointer.startY, w, h);
		speedUnit.layout(0, unitPointer.startY, w, h);
	}
	
	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
	}
	
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		int height = (int)DPIConvert.getDPI(100, getContext());
		int width = (int)DPIConvert.getDPI(80, getContext());
		setMeasuredDimension(height, width);
	}

	private class PointerLayout {
		public int startX;
		public int startY;
	
		public PointerLayout() {
			startX = 0;
			startY = 0;
		}
	}
	
}
